package com.stox.charting.ui.swing.model.price;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import com.stox.charting.ui.swing.model.Axis;
import com.stox.charting.ui.swing.model.ChartModel;
import com.stox.charting.ui.swing.model.Orientation;
import com.stox.charting.ui.swing.model.Theme;
import com.stox.charting.ui.swing.model.uiUnit.UiUnit;
import com.stox.charting.ui.swing.view.ChartView;
import com.stox.core.model.IBar;
import com.stox.core.util.Constant;

public class InfoAxis extends Axis implements MouseMotionListener {
	private static final long serialVersionUID = -7060993916117993196L;
	private static final int textHorizontalGap = 15;

	private ChartModel<? extends IBar> chartModel;
	private int uiUnitIndex;
	
	public InfoAxis(ChartView chartView, ChartModel<? extends IBar> chartModel) {
		super(chartView, Orientation.HORIZONTAL, BorderLayout.NORTH);
		setBackground(Color.CYAN);
		this.chartModel = chartModel;
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		for(UiUnit uiUnit : chartModel.getViewport().getUiUnits()){
			double x = e.getPoint().getX();
			if(x >= uiUnit.getX() && x <= (uiUnit.getX()+uiUnit.getWidth())){
				this.uiUnitIndex = uiUnit.getIndex();
				repaint();
				break;
			}
		}
	}
	
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		g.setFont(Theme.getFonts().getExtraLargeBoldFont());
		g.setColor(Theme.getColours().getAxisText());
		String text = chartModel.getData().get(0).getScrip().getName();
		int x = Theme.getStructure().getVerticalAxisWidth();
		double textHeight = g.getFontMetrics().getStringBounds(text, g).getHeight();
		double textWidth = g.getFontMetrics().getStringBounds(text, g).getWidth();
		int y = (int)(textHeight + (Theme.getStructure().getHorizontalAxisHeight()
				-  textHeight)/2);
		
		g.drawString(text, x, y);
		x += textWidth;
		
		if(uiUnitIndex < chartModel.getViewport().getEndIndex()){
			uiUnitIndex = chartModel.getViewport().getEndIndex();
		}
		if(uiUnitIndex > chartModel.getViewport().getStartIndex()){
			uiUnitIndex = chartModel.getViewport().getStartIndex();
		}
		
		IBar bar = chartModel.getData().get(uiUnitIndex);
		text = "Date : "+Constant.dateFormat.format(bar.getDate())
				+"    Open : "+bar.getOpen()
				+"    High : "+bar.getHigh()
				+"    Low : "+bar.getLow()
				+"    Close : "+bar.getClose()
				+"    Volume : "+bar.getVolume();
		g.setFont(Theme.getFonts().getMediumFont());
		textWidth = g.getFontMetrics().getStringBounds(text, g).getWidth();
		
		if(textWidth < (getVisibleRect().getWidth() 
				- Theme.getStructure().getVerticalAxisWidth() 
				- x 
				- textHorizontalGap)){
			g.drawString(text, x + textHorizontalGap, y);
		}
	}
	
}
